package com.mk.settlement.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.mk.settlement.common.FeeUtil;
import com.mk.settlement.enums.SettlementStateEnums;
import com.mk.settlement.enums.SettlementTargetIDEnums;
import com.mk.settlement.enums.SettlementTypeEnums;
import com.mk.settlement.mappers.SettlementResultMapper;
import com.mk.settlement.mappers.SettlementTempResultMapper;
import com.mk.settlement.model.SettlementResult;
import com.mk.settlement.service.SettlementRegularService;

/**
 * 阈值月结算服务类: 未人工进行阈值结算的月末自动结算.
 * @author chuaiqing.
 *
 */
@Service(value="settlementThresholdMonth2ResultService")
public class SettlementThresholdMonth2ResultService implements SettlementRegularService {

    private Logger log = LoggerFactory.getLogger(SettlementThresholdMonth2ResultService.class);
    
    @Resource
    private SettlementResultMapper settlementResultMapper;

    @Resource
    private SettlementTempResultMapper settlementTempResultMapper;
    
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
    
    @Override
    public void doSettlement() {
        
        log.info("阈值月结worker月终结算开始执行...");
        
        log.info("将所有未结算数据汇总到结果表...");
        
        List<SettlementResult> results = null;
        try {
            
            results = settlementResultMapper.getResultFromTemp(SettlementStateEnums.NotStatistics.getIndex(), sdf.format(new Date()),
                    SettlementTypeEnums.THRESHOLD_MONTH.getIndex());
            
            if(CollectionUtils.isEmpty(results)) {
                
                log.info("没有获取到需要汇总到结果表的数据.");
                
                return;
            }
            for (SettlementResult r : results) {

                if (r.getSettlementTargetId() >= SettlementTargetIDEnums.TRAVEL_AGENCY.getBegin()
                        && r.getSettlementTargetId() <= SettlementTargetIDEnums.TRAVEL_AGENCY.getEnd()) {

                    r.setSettlementSaleSum(FeeUtil.cal(r.getSettlementTotalSum(), 0));
                }

            }

            int result = settlementResultMapper.insertBatch(results);

            log.info("共向结果表插入" + result + "条数据.");
            
            
        } catch (Exception e) {
            log.error("将所有未结算数据汇总到结果表异常!", e);
            
            throw e;
        }
        log.info("汇总到结果表完毕.");
        
        log.info("更新汇总过的数据状态...");
        try {
            int result =  settlementTempResultMapper.updateAlreadyStatisticsStatusBatch(SettlementStateEnums.AlreadyStatistics.getIndex(), results);
            log.info("更新汇总过的数据状态" + result + "条.");
        } catch (Exception e) {
            log.error("更新汇总过的数据状态异常!", e);
            
            throw e;
        }
        log.info("更新汇总过的数据状态完毕.");
        
        log.info("阈值月结worker月终结算执行完毕.");
    }

}
